package com.yqg.protection.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.VideoDevice;
import com.yqg.protection.mapper.VideoDeviceMapper;
import com.yqg.protection.service.ISimInputStoreService;
import com.yqg.protection.service.IVideoDeviceService;
import com.yqg.protection.utils.Constants;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * VideoDeviceController 前端控制器
 * @author zxf
 * @since 2024-09-14
 */
@RestController
@RequestMapping("/video-device")
public class VideoDeviceController {

    @Resource
    private IVideoDeviceService videoDeviceService;

    @Resource
    private VideoDeviceMapper videoDeviceMapper;

    @Autowired
    private ISimInputStoreService simInputStoreService;
    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @SysLog("新增或者修改监控设备")
    @Transactional(rollbackFor = Exception.class)
    public Result save(@RequestBody VideoDevice videoDevice) {
        if (ObjectUtil.isEmpty(videoDevice.getId())) {
            LambdaQueryWrapper<VideoDevice> videoDeviceQueryWrapper = new LambdaQueryWrapper<>();
            videoDeviceQueryWrapper.eq(VideoDevice::getDeviceId, videoDevice.getDeviceId());
            videoDeviceQueryWrapper.eq(VideoDevice::getIsDelete, 0);
            VideoDevice videoDevice1 = videoDeviceService.getOne(videoDeviceQueryWrapper);
            if (videoDevice1 != null) {
                return Result.error(Constants.CODE_500, "该设备已存在");
            }
            videoDevice.setIsDelete(0);
        }
//        else {
//            VideoDevice videoDevice1 = videoDeviceService.getById(videoDevice.getId());
//            if (videoDevice1.getSimInputStoreId() != null && videoDevice1.getSimInputStoreId() != videoDevice.getSimInputStoreId()) {
//                SimInputStore simInputStore = simInputStoreService.getById(videoDevice1.getSimInputStoreId());
//                simInputStore.setDeviceBindingEquipment(0);
//                simInputStore.setEquipmentInputStoreId(null);
//                simInputStore.setDeviceBindingType(null);
//                simInputStoreService.updateById(simInputStore);
//            }
//        }
//        if (videoDevice.getSimInputStoreId() != null) {
//            videoDevice.setDeviceBindingSim(1);
//        } else {
//            videoDevice.setDeviceBindingSim(0);
//        }
        videoDeviceService.saveOrUpdate(videoDevice);
//        if (videoDevice.getDeviceBindingSim() == 1) {
//            SimInputStore simInputStore = simInputStoreService.getById(videoDevice.getSimInputStoreId());
//            simInputStore.setDeviceBindingEquipment(1);
//            simInputStore.setEquipmentInputStoreId(videoDevice.getId());
//            simInputStore.setDeviceBindingType(1);
//            simInputStoreService.updateById(simInputStore);
//        }
        return Result.success();
    }
    /**
    *  删除
    */
    @DeleteMapping("delete/{id}")
    @SysLog("删除监控设备")
    public Result delete(@PathVariable Integer id) {
        VideoDevice videoDevice =videoDeviceService.getById(id);
        videoDevice.setIsDelete(1);
        return Result.success(videoDeviceService.saveOrUpdate(videoDevice));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll(Integer level,
                          Long organizationId,
                          String isAllClient,
                          @RequestParam List<Long> clientIds,
                          Long clientId,
                          String videoName) {
       List<VideoDevice> list=  videoDeviceMapper.findAll(level, organizationId, isAllClient, clientIds, clientId, videoName);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(videoDeviceService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String clientName,
                           String name,
                           String deviceNum,
                           String monitorNum,
                           Integer level,
                           Long organizationId,
                           String isAllClient,
                           @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<VideoDevice> videoDevices = videoDeviceMapper.findPage(pageNum, pageSize, clientName, name, deviceNum, monitorNum, level, organizationId, isAllClient, clientIds);
        Long count = videoDeviceMapper.findPageCount(clientName, name, deviceNum, monitorNum, level, organizationId, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", videoDevices);
        map.put("total", count);
        return Result.success(map);
    }

    @GetMapping("/findNoBindingSIM")
    @ApiOperation("查询所有未绑定SIM卡的设备")
    public Result findNoBindingSIM(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   String deviceId,
                                   Integer level,
                                   Long organizationId,
                                   String isAllClient,
                                   @RequestParam List<Long> clientIds) {
        pageNum = pageNum * pageSize - pageSize;
        List<VideoDevice> videoDevices = videoDeviceMapper.findNoBindingSIMPage(pageNum, pageSize, deviceId, level, organizationId, isAllClient, clientIds);
        Long count = videoDeviceMapper.findNoBindingSIMPageCount(deviceId, level, organizationId, isAllClient, clientIds);
        HashMap<String, Object> map = new HashMap<>();
        map.put("record", videoDevices);
        map.put("total", count);
        return Result.success(map);
    }

    @GetMapping("/getByEquipmentId")
    @ApiOperation("根据监控的设备查询设备")
    public Result getByEquipmentId(@RequestParam String equipmentId) {
        LambdaQueryWrapper<VideoDevice> videoDeviceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        videoDeviceLambdaQueryWrapper.eq(VideoDevice::getIsDelete, 0);
        videoDeviceLambdaQueryWrapper.eq(VideoDevice::getEquipmentId, equipmentId);
        List<VideoDevice> list = videoDeviceService.list(videoDeviceLambdaQueryWrapper);
        if (list == null || list.size() == 0) {
            return Result.success(null);
        } else {
            VideoDevice videoDevice = list.get(0);
            return Result.success(videoDevice);
        }
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String clientName,
                       String name,
                       String deviceNum,
                       String monitorNum,
                       Integer level,
                       Long organizationId,
                       String isAllClient,
                       @RequestParam List<Long> clientIds) {
        videoDeviceService.export(response, clientName, name, deviceNum, monitorNum, level, organizationId, isAllClient, clientIds);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入监控设备")
    public void importData(MultipartFile file) {
        videoDeviceService.importData(file);
    }
}

